package com.lw.edu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lw.commonutil.result.Result;
import com.lw.edu.client.VodClient;
import com.lw.edu.entity.Video;
import com.lw.edu.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Handler;

@RestController
@RequestMapping("/admin/edu/video")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private VodClient vodClient;
    //添加小节
    @PostMapping("/addVideo")
    public Result addVideo(@RequestBody Video video){
        boolean b = videoService.save( video );
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //删除小节
    @DeleteMapping("{videoId}")
    public Result deleteVideo(@PathVariable String videoId){
        //得到视频id
        Video video = videoService.getById( videoId );
        //删除阿里云视频
        if (!video.getVideoSourceId().isEmpty()){
            //不为空删除
            vodClient.removeById( video.getVideoSourceId() );
        }
        boolean b = videoService.removeById( videoId );
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //修改小节
    @PostMapping("/updateVideo")
    public Result updateVideo(@RequestBody Video video){
        boolean b = videoService.updateById( video );
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //根据id查询小节
    @GetMapping("{videoId}")
    public Result getVideo(@PathVariable String videoId){
        Video video = videoService.getById( videoId );
        return Result.ok( video );
    }
    //rpc判断小节是否收费并且返回课程id
    @GetMapping("/inner/isFree/{videoId}")
    public Map vodIsFree(@PathVariable String videoId){
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq( "video_source_id", videoId );
        Video video = videoService.getOne( wrapper );
        Map map =new HashMap<>();
        if (video.getIsFree()){
            map.put( "isFree",true );
            map.put( "courseId",video.getCourseId() );
            return map;
        }else {
            map.put( "isFree",false );
            return map;
        }

    }
}
